<template>
	<div class="personal layout-pd">
		<el-row>
			<!-- 个人信息 -->
			<el-col :xs="24" :sm="24">
				<el-card shadow="hover" header="个人信息">
					<div class="personal-user">
						<div class="personal-user-left">
							<el-upload class="h100 personal-user-left-upload" :action="uploadUrl" :headers="headers"
								multiple :limit="1" :on-success="backageImagesOnSuccess">
								<img :src="personalForm.avatar" />
							</el-upload>
						</div>
						<div class="personal-user-right">
							<el-row>
								<el-col :span="24" class="personal-title mb18">{{ currentTime }}，admin，生活变的再糟糕，也不妨碍我变得更好！
								</el-col>
								<el-col :span="24">
									<el-row>
										<el-col :xs="24" :sm="8" class="personal-item mb6">
											<div class="personal-item-label">昵称：</div>
											<div class="personal-item-value">{{ personalForm.name }}</div>
										</el-col>
										<!-- <el-col :xs="24" :sm="16" class="personal-item mb6">
											<div class="personal-item-label">身份：</div>
											<div class="personal-item-value">超级管理</div>
										</el-col> -->
									</el-row>
								</el-col>
								<!-- <el-col :span="24">
									<el-row>
										<el-col :xs="24" :sm="8" class="personal-item mb6">
											<div class="personal-item-label">登录IP：</div>
											<div class="personal-item-value">192.168.1.1</div>
										</el-col>
										<el-col :xs="24" :sm="16" class="personal-item mb6">
											<div class="personal-item-label">登录时间：</div>
											<div class="personal-item-value">2021-02-05 18:47:26</div>
										</el-col>
									</el-row>
								</el-col> -->
							</el-row>
						</div>
					</div>
				</el-card>
			</el-col>

			<!-- 消息通知 -->
			<!-- <el-col :xs="24" :sm="8" class="pl15 personal-info">
				<el-card shadow="hover">
					<template #header>
						<span>消息通知</span>
						<span class="personal-info-more">更多</span>
					</template>
					<div class="personal-info-box">
						<ul class="personal-info-ul">
							<li v-for="(v, k) in state.newsInfoList" :key="k" class="personal-info-li">
								<a :href="v.link" target="_block" class="personal-info-li-title">{{ v.title }}</a>
							</li>
						</ul>
					</div>
				</el-card>
			</el-col> -->


			<!-- 更新信息 -->
			<el-col :span="24">
				<el-card shadow="hover" class="mt15 personal-edit" header="更新信息">
					<div class="personal-edit-title">基本信息</div>
					<el-form :model="personalForm" size="default" label-width="40px" class="mt35 mb35">
						<el-row :gutter="35">
							<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
								<el-form-item label="昵称">
									<el-input v-model="personalForm.name" placeholder="请输入昵称" clearable></el-input>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
								<el-form-item label="邮箱">
									<el-input v-model="personalForm.email" placeholder="请输入邮箱" clearable></el-input>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
								<el-form-item label="手机">
									<el-input v-model="personalForm.phone" maxlength="11" placeholder="请输入邮箱"
										clearable></el-input>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
								<el-form-item label="性别">
									<el-select v-model="personalForm.sex" placeholder="请选择性别" clearable class="w100">
										<el-option label="男" value="0"></el-option>
										<el-option label="女" value="1"></el-option>
									</el-select>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
								<el-form-item>
									<el-button type="primary" @click="updateUserInfo">
										<el-icon>
											<ele-Position />
										</el-icon>
										更新个人信息
									</el-button>
								</el-form-item>
							</el-col>
						</el-row>
					</el-form>
					<div class="personal-edit-title mb15">账号安全</div>
					<div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">账户密码</div>
								<div class="personal-edit-safe-item-left-value">当前密码强度：强</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button text type="primary" @click="handleOpenDialogPassword">立即修改</el-button>
							</div>
						</div>
					</div>
					<div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">企业/组织名称</div>
								<div class="personal-edit-safe-item-left-value">名称：{{enterpriseForm.name}}</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button text type="primary" @click="openenterprise">立即修改</el-button>
							</div>
						</div>
					</div>
					<div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">绑定微信</div>
								<div class="personal-edit-safe-item-left-value">已绑定微信：132****4108</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button text type="primary">立即设置</el-button>
							</div>
						</div>
					</div>
					<!-- <div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">绑定QQ</div>
								<div class="personal-edit-safe-item-left-value">已绑定QQ：110****566</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button text type="primary">立即设置</el-button>
							</div>
						</div>
					</div> -->
				</el-card>
			</el-col>
		</el-row>
	</div>



	<el-dialog v-model="dialogPasswordVisible" title="修改密码" width="500px" :before-close="handlePasswordClose">
		<el-form ref="ruleFormRef" :model="passworkForm" :rules="rules" label-width="120px" class="demo-ruleForm"
			:size="formSize" status-icon>
			<el-form-item label="旧密码" prop="oldPassword">
				<el-input v-model="passworkForm.oldPassword" type="password" show-password />
			</el-form-item>
			<el-form-item label="新密码" prop="newPassword">
				<el-input v-model="passworkForm.newPassword" type="password" show-password />
			</el-form-item>
			<el-form-item label="确认密码" prop="password">
				<el-input v-model="passworkForm.password" type="password" show-password />
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button size="small" @click="handlePasswordClose">取消</el-button>
				<el-button size="small" type="primary" @click="updatePwd(ruleFormRef)">
					确认
				</el-button>
			</span>
		</template>
	</el-dialog>

	<el-dialog v-model="dialogEnterpriseVisible" title="编辑企业/组织" width="500px" :before-close="handleEnterpriseClose">
		<el-form ref="ruleEnterpriseFormRef" :model="enterpriseForm" :rules="enterpriseRules" label-width="120px"
			class="demo-ruleForm" :size="formSize" status-icon>
			<el-form-item label="你的姓名" prop="legalUserName">
				<el-input v-model="enterpriseForm.legalUserName" />
			</el-form-item>
			<el-form-item label="企业或组织名称" prop="name">
				<el-input v-model="enterpriseForm.name" />
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button size="small" @click="handleEnterpriseClose">取消</el-button>
				<el-button size="small" type="primary" @click="updateEnterprise(ruleEnterpriseFormRef)">
					确认
				</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script  lang="ts" name="personal">
import { reactive, computed, onMounted, toRefs } from 'vue';
import { formatAxis } from '/@/utils/formatTime';
import { useUserInfo } from '/@/stores/userInfo'
import { storeToRefs } from 'pinia';
import api from '/@/api/commonApi'
import { updatePassword, updateUser } from '/@/api/sys/user'
import { getTypePage, getDataPage, createData, editData, deleteData } from '/@/api/sys/dict'
import Cookies from 'js-cookie'
import { Session, Local } from '/@/utils/storage'
import { ElMessage } from 'element-plus'
export default {
	setup(props, context) {
		const storeUserInfo = useUserInfo()
		const { userInfos } = storeToRefs(storeUserInfo)
		const validatePassConfim = (rule: any, value: any, callback: any) => {
			if (value !== state.passworkForm.newPassword) {
				callback(new Error("两次密码不一致!"))
			} else {
				callback()
			}
		}
		// 定义变量内容
		const state = reactive({
			uploadUrl: '/api/file/filaAttach/upload',
			headers: {
				'17work': Session.get('token')
			},
			personalForm: {
				avatar: null,
				name: null,
				sex: 0,
				id: null,
				email: null,
				phone: null
			},
			passworkForm: {
				userId: null,
				oldPassword: null,
				newPassword: null,
				password: null
			},
			dialogPasswordVisible: false,
			nowId: null,
			formSize: 'small',
			rules: {
				oldPassword: [
					{ required: true, message: '请输入旧密码', trigger: 'blur' },
					{ min: 6, max: 18, message: '请输入6-18位数', trigger: 'blur' },
				],
				newPassword: [
					{ required: true, message: '请输入新密码', trigger: 'blur' },
					{ min: 6, max: 18, message: '请输入6-18位数', trigger: 'blur' },
				],
				password: [
					{ required: true, message: '请输入确认密码', trigger: 'blur' },
					{ validator: validatePassConfim, trigger: 'blur' }
				],
			},
			dictTypeData: [
			],
			// 字典类型
			searchType: {
				current: 1,
				size: 10
			},
			total: 100,
			current: 1,
			page: 10,
			// 字典数据
			searchData: {
				current: 1,
				size: 30,
				enterpriseId: null,
				code: null
			},
			dictDataD: [],
			//更改企业信息
			enterpriseForm: {},
			dialogEnterpriseVisible: false,
			enterpriseRules: {
				legalUserName: [
					{ required: true, message: '请输入企业负责人', trigger: 'blur' }
				],
				name: [
					{ required: true, message: '请输入企业名称', trigger: 'blur' }
				],
			}
		});

		const getUserInfo = () => {
			state.nowId = Cookies.get("userId")
			api.getBelongerName({ userId: state.nowId }).then(res => {
				res.sex = res.sex + ""
				state.personalForm = res
			})
		}


		const getEnterpriseInfo = () => {
			api.enterpriseInfo().then(res => {
				res.data.name = null
				// state.sizeForm = res.data
			});
		}


		// 当前时间提示语
		const currentTime = computed(() => {
			return formatAxis(new Date());
		});
		const updateUserInfo = () => {
			state.personalForm.id = state.nowId
			updateUser(state.personalForm).then(res => {
				if (res.code == 0) {
					ElMessage.success("修改成功");
				}
			})

		};

		const handleOpenDialogPassword = () => {
			state.dialogPasswordVisible = true
		};

		const updatePwd = async (formEl) => {
			if (!formEl) return
			await formEl.validate((valid) => {
				if (valid) {
					state.passworkForm.userId = state.nowId
					updatePassword(state.passworkForm).then(res => {
						if (res.code == 0) {
							ElMessage.success("修改成功");
						}
					})
				}
			})
		};

		const handlePasswordClose = () => {
			state.dialogPasswordVisible = false
		};
		const backageImagesOnSuccess = (response, uploadFiles) => {
			state.personalForm.avatar = response.data.src
			if (response.code == 0) {
				state.personalForm.avatar = response.data.src
				updateUserInfo()
			}
		}
		const handleSizeChange = e => {
			state.searchType.size = e
			typePages()
		}
		const handleCurrentChange = e => {
			state.searchType.current = e
			state.current = e
			typePages()
		}
		const getDatas = (data) => {
			state.searchData.code = data.code
			getDataPage(state.searchData).then(res => {
				state.dictDataD = res.data.records
			})
		}



		const typePages = () => {
			getTypePage(state.searchType).then(res => {
				if (res.code == 0) {
					state.dictTypeData = res.data.records
					state.total = res.data.total
				}
			})
		};

		const openenterprise = () => {
			state.dialogEnterpriseVisible = true
		};
		const handleEnterpriseClose = () => {
			state.dialogEnterpriseVisible = false
			// state.enterpriseForm = {
			// 	legalUserName: null,
			// 	name: null,
			// 	id: null
			// }
		};

		const updateEnterprise = async (formEl) => {
			if (!formEl) return
			await formEl.validate((valid) => {
				if (valid) {
					api.enterpriseInfoEdit(state.enterpriseForm).then(res => {
						if (res.code == 0) {
							ElMessage({
								type: 'success',
								message: '更新成功',
								duration:1000,
								onClose: () => {
									handleEnterpriseClose()
								}
							})
						}
					})

				}
			})
		};
		const {
			ruleFormRef,
			ruleEnterpriseFormRef
		} = toRefs(props)

		onMounted(() => {
			getUserInfo()
			api.enterpriseInfo().then(res => {
				state.enterpriseForm = res.data
			});
		})
		return {
			currentTime,
			getEnterpriseInfo,
			getUserInfo,
			updateUser,
			handlePasswordClose,
			handleOpenDialogPassword,
			updatePwd,
			updateUserInfo,
			ruleFormRef,
			ruleEnterpriseFormRef,
			validatePassConfim,
			backageImagesOnSuccess,
			handleSizeChange,
			handleCurrentChange,
			getDatas,
			openenterprise,
			handleEnterpriseClose,
			updateEnterprise,
			...toRefs(state),
		}
	}
}












</script>

<style scoped lang="scss">
@import '../../theme/mixins/index.scss';

.personal {
	.personal-user {
		height: 130px;
		display: flex;
		align-items: center;

		.personal-user-left {
			width: 100px;
			height: 130px;
			border-radius: 3px;

			:deep(.el-upload) {
				height: 100%;
			}

			.personal-user-left-upload {
				img {
					width: 100%;
					height: 100%;
					border-radius: 3px;
				}

				&:hover {
					img {
						animation: logoAnimation 0.3s ease-in-out;
					}
				}
			}
		}

		.personal-user-right {
			flex: 1;
			padding: 0 15px;

			.personal-title {
				font-size: 18px;
				@include text-ellipsis(1);
			}

			.personal-item {
				display: flex;
				align-items: center;
				font-size: 13px;

				.personal-item-label {
					color: var(--el-text-color-secondary);
					@include text-ellipsis(1);
				}

				.personal-item-value {
					@include text-ellipsis(1);
				}
			}
		}
	}

	.personal-info {
		.personal-info-more {
			float: right;
			color: var(--el-text-color-secondary);
			font-size: 13px;

			&:hover {
				color: var(--el-color-primary);
				cursor: pointer;
			}
		}

		.personal-info-box {
			height: 130px;
			overflow: hidden;

			.personal-info-ul {
				list-style: none;

				.personal-info-li {
					font-size: 13px;
					padding-bottom: 10px;

					.personal-info-li-title {
						display: inline-block;
						@include text-ellipsis(1);
						color: var(--el-text-color-secondary);
						text-decoration: none;
					}

					& a:hover {
						color: var(--el-color-primary);
						cursor: pointer;
					}
				}
			}
		}
	}

	.personal-recommend-row {
		.personal-recommend-col {
			.personal-recommend {
				position: relative;
				height: 100px;
				border-radius: 3px;
				overflow: hidden;
				cursor: pointer;

				&:hover {
					i {
						right: 0px !important;
						bottom: 0px !important;
						transition: all ease 0.3s;
					}
				}

				i {
					position: absolute;
					right: -10px;
					bottom: -10px;
					font-size: 70px;
					transform: rotate(-30deg);
					transition: all ease 0.3s;
				}

				.personal-recommend-auto {
					padding: 15px;
					position: absolute;
					left: 0;
					top: 5%;
					color: var(--next-color-white);

					.personal-recommend-msg {
						font-size: 12px;
						margin-top: 10px;
					}
				}
			}
		}
	}

	.personal-edit {
		.personal-edit-title {
			position: relative;
			padding-left: 10px;
			color: var(--el-text-color-regular);

			&::after {
				content: '';
				width: 2px;
				height: 10px;
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				background: var(--el-color-primary);
			}
		}

		.personal-edit-safe-box {
			border-bottom: 1px solid var(--el-border-color-light, #ebeef5);
			padding: 15px 0;

			.personal-edit-safe-item {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.personal-edit-safe-item-left {
					flex: 1;
					overflow: hidden;

					.personal-edit-safe-item-left-label {
						color: var(--el-text-color-regular);
						margin-bottom: 5px;
					}

					.personal-edit-safe-item-left-value {
						color: var(--el-text-color-secondary);
						@include text-ellipsis(1);
						margin-right: 15px;
					}
				}
			}

			&:last-of-type {
				padding-bottom: 0;
				border-bottom: none;
			}
		}
	}
}
</style>
